package com.hht.baselibs.util

import android.util.SparseArray
import android.view.View
import android.widget.TextView
import androidx.annotation.IdRes


/**
 * Author:chen'hu
 * Time:2023/8/9 11:51
 * Description:This is ViewUtils
 */
class ViewUtils private constructor(view: View) {
    private var views: SparseArray<View> = SparseArray()
    private var convertView: View

    init {
        convertView = view
    }

    companion object {
        fun create(view: View): ViewUtils {
            return ViewUtils(view)
        }
    }

    /**
     * 获取View
     *
     * @param viewId
     * @param <T>
     * @return
    </T> */
    fun <T : View?> getView(@IdRes viewId: Int): T? {
        var view = views[viewId]
        if (view == null) {
            view = convertView.findViewById(viewId)
            views.put(viewId, view)
        }
        return view as T?
    }

    /**
     * 设置文本
     *
     * @param viewId
     * @param text
     */
    fun setText(viewId: Int, text: String?) {
        val textView = getView<TextView>(viewId)!!
        textView.text = text
    }

    /**
     * 设置字体颜色
     *
     * @param viewId
     * @param colorId
     */
    fun setTextColor(viewId: Int, colorId: Int) {
        val textView = getView<TextView>(viewId)!!
        textView.setTextColor(colorId)
    }

    /**
     * 设置背景图片
     *
     * @param viewId
     * @param resId
     */
    fun setBackgroundResource(viewId: Int, resId: Int) {
        val view = getView<View>(viewId)!!
        view.setBackgroundResource(resId)
    }

    /**
     * 设置背景颜色
     *
     * @param viewId
     * @param colorId
     */
    fun setBackgroundColor(viewId: Int, colorId: Int) {
        val view = getView<View>(viewId)!!
        view.setBackgroundColor(colorId)
    }

    /**
     * 设置点击事件
     *
     * @param viewId
     * @param listener
     */
    fun setOnClickListener(viewId: Int, listener: View.OnClickListener?) {
        val view = getView<View>(viewId)!!
        view.setOnClickListener(listener)
    }

}